/*
 * Copyright (c) sl All rights reserved.
 * 本软件源代码版权归sl所有,未经许可不得任意复制与传播.
 */
package com.sl.crawler.web.controller;

import com.sl.crawler.common.utils.DateUtils;
import com.sl.crawler.domain.HouseDetailDay;
import com.sl.crawler.domain.common.Page;
import com.sl.crawler.service.HouseDetailDayService;
import com.sl.crawler.service.base.BaseService;
import com.sl.crawler.web.BaseController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author sl
 */
@Controller
@RequestMapping(value = "/houseDetailDay")
public class HouseDetailDayController extends BaseController {
	private static final Logger LOG = LoggerFactory.getLogger(HouseDetailDayController.class);
	@Autowired
	private HouseDetailDayService houseDetailDayService;

	/**
	* 获取Service操作类
	*/
	@Override
	public BaseService<HouseDetailDay, Long> getService() {
	  return houseDetailDayService;
	}

	/**
	* 获取视图路径
	*/
	@Override
	public String getModelName() {
	  return "houseDetailDay";
	}

	/**
	 * 列表展示
	 * @param houseDetailDay 实体对象
	 * @param page 分页对象
	 * @return
	 */
	@RequestMapping(method = {RequestMethod.GET,RequestMethod.POST})
	public String list(HouseDetailDay houseDetailDay,Page<HouseDetailDay> page,Model view) throws Exception{
		return baseList(houseDetailDay, page, view);
	}

	/**
	 * 响应新增(修改)页面
	 * @param id 对象编号
	 * @return
	 */
	@RequestMapping(value="/edit/{id}")
	public String edit(@PathVariable Long id,Model view) throws Exception{
		return baseEdit(id, view);
	}

	/**
	 * 通过编号删除对象
	 * @param id 对象编号
	 * @return
	 */
	@RequestMapping(value="/delete/{id}")
	public String delete(@PathVariable Long id,Model view) throws Exception{
		return baseDelete(id, view);
	}

	/**
	 * 通过编号查看对象
	 * @param id 对象编号
	 * @return
	 */
	@RequestMapping(value="/{id}",method=RequestMethod.GET)
	public String view(@PathVariable Long id,Model view) throws Exception{
		return baseView(id, view);
	}

	/**
	 * 保存方法
	 * @param houseDetailDay 实体对象
	 * @return
	 */
	@RequestMapping(value="/save",method = {RequestMethod.POST,RequestMethod.GET})
	public String save(HouseDetailDay houseDetailDay,Model view) throws Exception{
		return baseSave(houseDetailDay);
	}


	/**
	* 导出CSV
	* @param houseDetailDay
	* @return
	* @throws Exception
	*/
	@RequestMapping(value = "exportCsv")
	public void exportCsv(@ModelAttribute("houseDetailDay") HouseDetailDay houseDetailDay, HttpServletResponse response) throws Exception {
    	baseExportCsv(houseDetailDay, new Page<HouseDetailDay>(1, 5000), response);
	}

	/**
	* 获取导出csv的表头
	* @return
	*/
	@Override
	public String getExportCsvTitle(){
		String title = "主键,爬取日期,总价,单价,关注人数,带看人数,小区,标题,建筑面积,房屋编号,是否有效,备注,修改人,创建时间,修改时间,";
		return title;
	}

	/**
	* 获取导出csv的数据明细
	* @param o
	* @return
	*/
	@Override
	public String getExportCsvRow(Object o) {
		HouseDetailDay houseDetailDay = (HouseDetailDay)o;
		StringBuilder sb = new StringBuilder();
    	sb.append("\"").append(houseDetailDay.getId()).append("\"").append(",");
    	sb.append(houseDetailDay.getCrawlerDate()).append(",");
    	sb.append("\"").append(houseDetailDay.getTotal()).append("\"").append(",");
    	sb.append("\"").append(houseDetailDay.getUnitPrice()).append("\"").append(",");
    	sb.append("\"").append(houseDetailDay.getFollowCount()).append("\"").append(",");
    	sb.append("\"").append(houseDetailDay.getLookCount()).append("\"").append(",");
    	sb.append(houseDetailDay.getXiaoqu()).append(",");
    	sb.append(houseDetailDay.getTitle()).append(",");
    	sb.append("\"").append(houseDetailDay.getHouseArea()).append("\"").append(",");
    	sb.append(houseDetailDay.getHouseId()).append(",");
    	sb.append("\"").append(houseDetailDay.getYn()).append("\"").append(",");
    	sb.append(houseDetailDay.getRemark()).append(",");
    	sb.append(houseDetailDay.getModifiedUser()).append(",");
    	sb.append(DateUtils.format(houseDetailDay.getCreatedDate())).append(",");
    	sb.append(DateUtils.format(houseDetailDay.getModifiedDate())).append(",");
		sb.append("\r\n");
		return sb.toString();
	}

	//自定义扩展

	/**
	 * ajax获取房屋明细列表
	 * @param houseDetailDay 实体对象
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/getAjaxHouseDetail",method = {RequestMethod.POST,RequestMethod.GET})
	public List<HouseDetailDay> getAjaxHouseDetail(HouseDetailDay houseDetailDay) throws Exception{
		return houseDetailDayService.selectEntryList(houseDetailDay);
	}

}